package io.yuncheng.modules.sys.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import io.yuncheng.common.utils.Constant;
import io.yuncheng.common.utils.NumberUtil;
import lombok.Data;

/**
 * 系统日志
 *
 * @author jared
 * @email 15721509625@163.com
 * @date 2020-11-26 20:39:57
 */
@Data
@TableName("sub_app_log")
public class SubAppLogEntity implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 *
	 */
	@TableId
	private Long id;
	/**
	 * 应用id
	 */
	private String appUniqueId;
	/**
	 * 日志类型
	 */
	private String logType;
	/**
	 * 单号
	 */
	private String transactionId;
	/**
	 * 状态
	 */
	private String logStatus;
	/**
	 * 状态描述
	 */
	private String logStatusMsg;
	/**
	 * 请求参数
	 */
	private String actionParameters;
	/**
	 * 步骤列表
	 */
	private String steps;
	/**
	 * 日志生成的时间
	 */
	private Date logDate;
	/**
	 * 执行时长(毫秒)
	 */
	private Long exeTime;
	/**
	 * 创建时间
	 */
	private Date createDate;

	public  String buildStepPath(){
		Long exeTime = 0L;
		String path = "";
		if(this.getSteps()!=null){
			List<AgentArithmeticStepEntity> list = JSON.parseObject(this.getSteps(), new TypeReference<List<AgentArithmeticStepEntity>>() {
			});
			Collections.sort(list ,(a1,a2)->a1.getStepId().compareTo(a2.getStepId()));
			for (AgentArithmeticStepEntity entity : list) {
				if(path.equals("")){
					path = entity.getStepAlgo();
				}else {
					path = path + Constant.COMMA + entity.getStepAlgo();
				}
				exeTime = NumberUtil.longAdd(exeTime, entity.getStepTimeSpan());
			}
			setExeTime(exeTime);
		}
		return path;
	}
}
